<template>
    <div>
        <!--顶部tabbar选项模块-->
        <div id="slider" class="mui-slider" data-slider="4">
            <div id="sliderSegmentedControl" class="mui-scroll-wrapper mui-slider-indicator mui-segmented-control mui-segmented-control-inverted">
                <!--区域滚动的模块-->
                <div class="mui-scroll">
                    <a @click="getPhotoList(item.id)" v-for="(item,i) in cates" :key="item.id" :class="['mui-control-item',item.id==0?'mui-active':'']" href="#item1mobile">
                        {{item.title}}
                    </a>
                </div>
            </div>
        </div>

        <!--图片列表区域-->
        <ul class="photoList">
            <router-link :to="'/home/photoInfo/'+item.id" v-for="item in imageArr" :key="item.id" tag="li">
                <img v-lazy="item.img_url">
                <div class="photoTitle">
                    <h4>{{item.title}}</h4>
                    <div v-html="item.zhaiyao" class="photo_body"></div>
                </div>
            </router-link>
        </ul>
    </div>
</template>

<script>
    /*局部导入js文件*/
    import mui from '../../lib/mui/js/mui.min'
    //报错 分析：1.mui.js可能用到了caller,callee,arguments等属性，但是webpack打包好的main.js中，可能默认启用了严格模式，所以两者冲突
    //解决方案 1：把mui.js中的非严格模式改掉，不现实
    //         2.把webpack打包时的严格模式禁用掉，使用插件
    export default {
        name: "photoList",
        data(){
            return {
                cates:[],
                imageArr:[]
            }
        },
        mounted(){//在mounted里面操作，代表你的dom元素已经渲染完成
            //2.初始化滑动控件
            mui('.mui-scroll-wrapper').scroll({
                deceleration: 0.0005
            });
            //3.tabbar栏失效，样式与导入的mui.js冲突，修改了mui-tab-item的类名，样式不变
        },
        methods:{
            //4.获取图片分类的数据
            getCategory(){
                this.$http.get('api/getimgcategory').then( result => {
                    // console.log(result)
                    if (result.body.status === 0){
                        result.body.message.unshift({title:'全部',id:0})
                        this.cates = result.body.message;
                    }
                })
            },
            //5.点击图片分类显示对应的图片列表
            getPhotoList(id){
                this.$http.get('api/getimages/'+id).then(result => {
                    if (result.body.status === 0){
                        // console.log(result)
                        this.imageArr = result.body.message;
                    }
                })
            }
        },
        created(){
            this.getCategory();
            //刷新时触发
            this.getPhotoList(0);//默认id为0
        }
    }
</script>

<style scoped lang="less">
    *{
        touch-action: pan-y;
    }
    .photoList{
        list-style: none;
        padding: 5px;
        margin: 0;
        li{
            background-color: #ccc;
            text-align: center;
            margin-bottom: 10px;
            box-shadow: 0 0 10px #999;
            position: relative;
            img{
                display: block;
                width: 100%;
                height: 100%;
            }
            //懒加载图片的样式
            img[lazy=loading] {
                width: 40px;
                height: 300px;
                margin:auto;
            }
            .photoTitle{
                height: 84px;
                color: white;
                text-align: left;
                position: absolute;
                bottom: 0;
                background-color: rgba(0, 0, 0, 0.4);
                h4{
                    font-size: 14px;
                }
                .photo_body{
                    font-size: 13px;
                }
            }
        }
    }

</style>